package com.jeffse.light.stream.core.data.context;

import com.jeffse.light.stream.core.pojo.WaterMark;
import com.jeffse.light.stream.core.service.SimpleTimerService;
import com.jeffse.light.stream.core.service.TimerService;
import lombok.extern.slf4j.Slf4j;

import java.util.UUID;

@Slf4j
public abstract class TimerContext<KEY, T, O> implements KeyedContext<KEY> {
    private SimpleTimerService timerService = new SimpleTimerService(this);
    private String uuid = UUID.randomUUID().toString();
    private KEY currentKey;

    protected ProcessContext<O> processContext;

    public void setProcessContext(ProcessContext processContext){
        this.processContext = processContext;
    }

    public ProcessContext getProcessContext(){
        return processContext;
    }

    @Override
    public KEY getCurrentKey() {
        return currentKey;
    }

    public void setCurrentKey(KEY currentKey) {
        this.currentKey = currentKey;
    }


    @Override
    public String getUUID(){
        return this.uuid;
    }

    public void onTimer(long timestamp, KeyedContext<KEY> ctx, ProcessContext<O> processContext) throws Exception {
//        log.warn("onTimer 方法需要被重写");
    }

    public TimerService getTimerService(){
        return timerService;
    }

    /**
     * 每次进入数据都会调用，进行 WaterMark 的计算和触发
     */
    public void WaterMark(WaterMark waterMark){
        timerService.waterMark(waterMark);
    }


    public void triggerEvent() throws Exception {
        timerService.triggerEvent();
    }
}
